package org.xfy.ymcc.controller;

import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.xfy.common.constant.KillConst;
import org.xfy.ymcc.domain.KillCourse;
import org.xfy.ymcc.dto.KillDto;
import org.xfy.ymcc.service.IKillActivityService;
import org.xfy.ymcc.domain.KillActivity;
import org.xfy.ymcc.query.KillActivityQuery;
import org.xfy.common.result.JsonResult;
import org.xfy.common.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/killActivity")
public class KillActivityController {

    @Autowired
    public IKillActivityService killActivityService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/kill")
    public JsonResult kill(@RequestBody KillDto killDto, HttpServletRequest request){
        return killActivityService.kill(killDto,request);
    }


    /**
     * 从redis中的hash中获取全部的秒杀课程数据
     * @return
     */
    @GetMapping("/getListFromRedis")
    public JsonResult listFromRedis() {
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(KillConst.KEY_KILL_COURSE);
        return JsonResult.success().setData(hashOps.values());
    }

    /**
     * 从Redis的hash结构中通过id查找某个秒杀课程
     * @param killCourseId
     * @return
     */
    @GetMapping("/getListFromRedis/{killCourseId}")
    public JsonResult getByIdFromRedis(@PathVariable("killCourseId") Long killCourseId) {
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(KillConst.KEY_KILL_COURSE);
        return JsonResult.success().setData(hashOps.get(killCourseId.toString()));
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody KillActivity killActivity){
        if(killActivity.getId()!=null){
            killActivityService.updateById(killActivity);
        }else{
            killActivityService.save(killActivity);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        killActivityService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(killActivityService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(killActivityService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody KillActivityQuery query){
        Page<KillActivity> page = new Page<KillActivity>(query.getPageNo(), query.getPageSize());
        page = killActivityService.page(page);
        return JsonResult.success().setData(new PageList<KillActivity>(page.getTotal(), page.getRecords()));
    }
}
